package com.example.administrator.myapplication;



import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;


public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";

    TextView mTvResult;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mTvResult = findViewById(R.id.tv_result);
        mTvResult.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });

        AlertDialog dialog = new AlertDialog.Builder(this)
                .setTitle("请选择性别")
                .setPositiveButton("确定",null)
                .setNegativeButton("取消",null)
                .setIcon(R.mipmap.ic_launcher)
                .setSingleChoiceItems(new String[]{"男","女"},0,
                        new DialogInterface.OnClickListener(){
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                Log.e(TAG, "onClick: "+which );
                                //将用户的选择显示到Activity中
                                //获取显示数据的控件
                                TextView tvResult = findViewById(R.id.tv_result);
                                //根据选项数据，显示内容
                                tvResult.setText(which==0?"男":"女");
                                dialog.dismiss();//关闭对话框
                                
                            }
                        }
                        )
                .create();


    }
}

